using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using GeoFramework.IO;

namespace GeoFramework.Examples
{
    public partial class MonitoringDetectingDevicesForm : Form
    {
        public MonitoringDetectingDevicesForm()
        {
            InitializeComponent();

            // Bind into all events for device detection
            Devices.DeviceDetectionStarted += new EventHandler(Devices_DeviceDetectionStarted);
            Devices.DeviceDetectionAttempted += new DeviceEventHandler(Devices_DeviceDetectionAttempted);
            Devices.DeviceDetected += new DeviceEventHandler(Devices_DeviceDetected);
            Devices.DeviceDetectionCanceled += new DeviceEventHandler(Devices_DeviceDetectionCancelled);
            Devices.DeviceDetectionCompleted += new EventHandler(Devices_DeviceDetectionCompleted);
            Devices.DeviceDetectionExceptionOccurred += new ExceptionEventHandler(Devices_ExceptionOccurred);

            // Enable/disable the Detect and Cancel buttons depending on whether
            // detection is already in progress
            if (Devices.IsDetectionInProgress)
            {
                DetectButton.Enabled = false;
                CancelDetectionButton.Enabled = true;
            }
            else
            {
                DetectButton.Enabled = true;
                CancelDetectionButton.Enabled = false;
            }
        }

        #region Starting and Canceling Device Detection

        private void DetectButton_Click(object sender, System.EventArgs e)
        {
            // Start device detection
            Devices.Detect();

            // Disable Detect, enable Cancel
            DetectButton.Enabled = false;
            CancelDetectionButton.Enabled = true;
        }

        private void CancelDetectionButton_Click(object sender, System.EventArgs e)
        {
            // Cancel detection
            Devices.CancelDetection();

            // Disable Cancel, enable Detect
            DetectButton.Enabled = false;
            CancelDetectionButton.Enabled = true;
        }

        #endregion

        #region Device Detection Events

        private void Devices_DeviceDetectionStarted(object sender, EventArgs e)
        {
            // Show that detection has started
            MessageListBox.Items.Add("Starting device detection.");
        }

        private void Devices_DeviceDetectionAttempted(object sender, DeviceEventArgs e)
        {
            // Show that detection has started for the device
            MessageListBox.Items.Add("Attempting detection on " + e.Device.ToString());
        }

        private void Devices_DeviceDetected(object sender, DeviceEventArgs e)
        {
            // Show that the device was detected
            MessageListBox.Items.Add("Found GPS Device: " + e.Device.ToString());
        }

        private void Devices_DeviceDetectionCancelled(object sender, DeviceEventArgs e)
        {
            // Show that the device was cancelled
            MessageListBox.Items.Add("Cancelled detection on " + e.Device.ToString());
        }

        private void Devices_DeviceDetectionCompleted(object sender, EventArgs e)
        {
            // Show that detection has started
            MessageListBox.Items.Add("Device detection completed.");

            // Re-enable the Detect button
            DetectButton.Enabled = true;
            CancelDetectionButton.Enabled = false;
        }

        private void Devices_ExceptionOccurred(object sender, ExceptionEventArgs e)
        {
            // Show that detection has started
            MessageListBox.Items.Add("ERROR: " + e.Exception.ToString());
        }

        #endregion
    }
}